package com.xydtech.web.controller.system;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.DictUtils;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.system.domain.SysLoanCalculator;
import com.xydtech.system.service.ISysDeptService;
import com.xydtech.system.service.ISysLoanCalculatorService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <dd>描述: ~ 贷款计算器信息Controller</dd>
 * <dd>创建时间：  10:04 2024/1/20</dd>
 * <dd>创建人： yuzhiguo</dd>
 * <dt>版本历史: </dt>
 * Date         Author         Version     Description
 * ------------------------------------------------------------------
 * 2024/1/20     yuzhiguo         1.0        1.0 Version
 */
@Controller
@RequestMapping("/system/calculator")
public class SysLoanCalculatorController extends BaseController {
    private String prefix = "system/calculator";

    @Autowired
    private ISysLoanCalculatorService sysLoanCalculatorService;
    @Autowired
    private ISysDeptService sysDeptService;

    @RequiresPermissions("system:calculator:view")
    @GetMapping()
    public String calculator() {
        return prefix + "/calculator";
    }

    /**
     * 查询贷款计算器信息列表
     */
    @RequiresPermissions("system:calculator:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysLoanCalculator sysLoanCalculator) {
        startPage();
        List<SysLoanCalculator> list = sysLoanCalculatorService.selectSysLoanCalculatorList(sysLoanCalculator);
        return getDataTable(list);
    }

    /**
     * 导出贷款计算器信息列表
     */
    @RequiresPermissions("system:calculator:export")
    @Log(title = "贷款计算器信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysLoanCalculator sysLoanCalculator) {
        String salt = getSalt();//盐值
        List<SysLoanCalculator> list = sysLoanCalculatorService.selectSysLoanCalculatorList(sysLoanCalculator);
        for (SysLoanCalculator loanCalculator : list) {
            //处理商户类型
            String merchantType = DictUtils.getDictLabel("sys_calculator_merchant_type", loanCalculator.getMerchantType());
            loanCalculator.setMerchantType(merchantType);
            //处理小程序类型
            String appletType = DictUtils.getDictLabel("sys_calculator_applet_type", loanCalculator.getAppletType());
            loanCalculator.setAppletType(appletType);
            //处理法人机构
            SysDept sysDept = sysDeptService.selectDeptById(loanCalculator.getDeptId(), salt);
            loanCalculator.setDeptName(sysDept.getDeptName());
        }
        ExcelUtil<SysLoanCalculator> util = new ExcelUtil<SysLoanCalculator>(SysLoanCalculator.class);
        return util.exportExcel(list, "贷款计算器信息数据");
    }

    /**
     * 新增贷款计算器信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存贷款计算器信息
     */
    @RequiresPermissions("system:calculator:add")
    @Log(title = "贷款计算器信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysLoanCalculator sysLoanCalculator) {
        sysLoanCalculator.setCreateBy(getLoginName());
        List<SysLoanCalculator> sysLoanCalculatorList = sysLoanCalculatorService.selectSysLoanCalculatorList(sysLoanCalculator);
        if (sysLoanCalculatorList.size() == 0){
            return toAjax(sysLoanCalculatorService.insertSysLoanCalculator(sysLoanCalculator));
        }else {
            return AjaxResult.error("该机构下已存在此配置");
        }

    }

    /**
     * 修改贷款计算器信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        SysLoanCalculator sysLoanCalculator = sysLoanCalculatorService.selectSysLoanCalculatorById(id);
        mmap.put("sysLoanCalculator", sysLoanCalculator);
        return prefix + "/edit";
    }

    /**
     * 修改保存贷款计算器信息
     */
    @RequiresPermissions("system:calculator:edit")
    @Log(title = "贷款计算器信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysLoanCalculator sysLoanCalculator) {
        sysLoanCalculator.setUpdateBy(getLoginName());
        return toAjax(sysLoanCalculatorService.updateSysLoanCalculator(sysLoanCalculator));
    }

    /**
     * 删除贷款计算器信息
     */
    @RequiresPermissions("system:calculator:remove")
    @Log(title = "贷款计算器信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(sysLoanCalculatorService.deleteSysLoanCalculatorByIds(ids));
    }

}
